<?php

/**
 * @Author: Mr.Mao
 * @Date:   2021-05-24 14:27:07
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-05-26 11:59:08
 */
namespace app\weixin\controller\sys_admin;
use app\AdminController;
use app\weixin\model\WeiXinMpModel;
use app\weixin\model\WeixinVideoBrandModel;
/**
 * 自定义交易组件品牌管理
 * Class Index
 */
class VideoBrand extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
        $this->Model = new WeixinVideoBrandModel();
        $this->assign('exStatus',[0=>'审核中',1=>'<font color="green">已通过</font>',9=>'<font color="red">未通过</font>']);
    }
    /**
     * [index 首页]
     * @return [type] [description]
     */
    public function index()
    {
    	// $res = $this->Model->getBrandList();
    	$this->getList(true);
    	return $this->fetch('index');
    }
    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {
    	// $this->Model->getBrandList();
        $where = [];

        $keyword = input('keyword','','trim');

        if (empty($keyword) == false) {
            $where[] = ['name','like','%'.$keyword.'%'];
        }
       
        $data = $this->getPageList($this->Model,$where,'id');

        foreach ($data['list'] as $key => $value) {
        	$value = $this->Model->info($value);
        	$data['list'][$key] = $value;
        }

        $this->assign("data", $data);
        if ($runData == false){
            $data['content']= $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }

    public function info()
    {
    	$id = input('id');
    	if ($id < 1) {
    		return $this->error('传参错误.');
    	}
    	$row = $this->Model->info($id);
    	if (is_array($row) == false) {
    		return $this->error($row);
    	}
    	$this->assign('row',$row);
    	return $this->fetch('info');
    	
    }
    /**
     * [edit 编辑]
     */
    public function edit()
    {
    	$id = input('id');
    	if ($id < 1) {
    		return $this->error('传参错误');
    	}
    	$info = $this->Model->find($id);
    	if (empty($info)) {
    		return $this->error('品牌不存在');
    	}

    	$post = input();
		$res = $this->Model->auditBrand($post);
		if ($res['errcode'] > 0) {
			return $this->error($res['errmsg']);
		}
		$upData = [];
		$upData = $post['audit_req'];
		$upData['add_time'] = time();
		$upData['status'] = 0;
		$upData['name'] = $post['audit_req']['brand_info']['brand_wording'];
		$upData['brand_info'] = json_encode($upData['brand_info']);
		$upData['audit_id'] = $res['audit_id'];
		$res = $this->Model->where('id',$info['id'])->update($upData);
		if ($res < 1) {
			return $this->error('提交失败.');
		}
		return $this->success('提交成功.',url('index'));
    }

    /**
     * [add 添加品牌]
     */
    public function add()
    {
    	
    	if ($this->request->isPost()) {
    		$post = input();
    		$res = $this->Model->auditBrand($post);
    		if ($res['errcode'] > 0) {
    			return $this->error($res['errmsg']);
    		}
    		$inArr = [];
    		$inArr = $post['audit_req'];
    		$inArr['add_time'] = time();
    		$inArr['status'] = 0;
    		$inArr['name'] = $post['audit_req']['brand_info']['brand_wording'];
    		$inArr['brand_info'] = json_encode($inArr['brand_info']);
    		$inArr['audit_id'] = $res['audit_id'];
    		$res = $this->Model->save($inArr);
    		if ($res < 1) {
    			return $this->error('添加失败.');
    		}
    		return $this->success('提交成功.',url('index'));
    	}
    	return $this->fetch('add');
    }
}